#coding=utf-8
from google.appengine.ext import db
from google.appengine.api import mail
from string import Template
  
class Ad(db.Model):
  campaign = db.StringProperty(required=True)
  subject = db.StringProperty(required=True)
  plain_content = db.TextProperty(default='hello')
  html_content = db.TextProperty(required=True)

  flag = db.BooleanProperty(default=False)
  
  created_at = db.DateTimeProperty(auto_now_add=True)
  
  def send_to(self, sender, to):
    name = to.split('@')[0]
    
    subject = Template(self.subject).substitute(name=name)
    
    body = Template(self.plain_content).substitute(email=to)
    html = Template(self.html_content).substitute(email=to)
    
    mail.send_mail(sender=sender,
      to=to,
      subject=subject,
      body=body, 
      html=html)

  def to_html(self, email):
    return Template(self.html_content).substitute(email=email)
